package com.xzc._3_running;

import com.xzc._1_build.Base_Test;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;

/**
 * 无状态session，内部是一个实例，单独存在，运行一次就会销毁，自动dispose
 */
public class StatelessSessionTest {

    Base_Test t = new Base_Test();
    KieBase kbase;
    KieSession ksession;

    public void init() {
        t.createContainerByClasspath();
        kbase = t.getKieContainer().getKieBase("KBase1");
        ksession = t.getKieContainer().newKieSession("KSession2_1");
    }

    /**
     * 设置全局变量,所有的session共享，在用的session,execution
     */
    public void setGlobal() {
        StatelessKieSession ksession = kbase.newStatelessKieSession();
// Set a global hbnSession, that can be used for DB interactions in the rules.
        ksession.setGlobal("hbnSession", "hibernateSession");
// Execute while being able to resolve the "hbnSession" identifier.
        ksession.execute("collection");
    }
}
